package com.wxmit.framework.data.permission.config;

import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.handler.DataPermissionHandler;
import com.baomidou.mybatisplus.extension.plugins.inner.DataPermissionInterceptor;
import com.wxmit.framework.data.permission.core.aspectj.DataPermissionAspect;
import com.wxmit.framework.data.permission.core.handler.DataPermissionHandlerImpl;
import com.wxmit.framework.mybatis.core.util.MyBatisUtils;
import com.wxmit.system.api.PermissionApi;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Description 数据权限的自动配置类
 * @Author huangxiong
 * @Date 2022/7/15 09:44
 * @Version V1.0
 */
@Configuration
public class ShushanDataPermissionAutoConfiguration {

    @Bean
    public DataPermissionAspect getDataPermissionContextHolder(){
        return new DataPermissionAspect();
    }

    @Bean
    public DataPermissionInterceptor dataPermissionDatabaseInterceptor(MybatisPlusInterceptor interceptor,
                                                                               PermissionApi permissionApi) {
        DataPermissionHandler dataPermissionHandler = new DataPermissionHandlerImpl(permissionApi);
        DataPermissionInterceptor inner = new DataPermissionInterceptor(dataPermissionHandler);
        // 添加到 interceptor 中
        // 需要加在首个，主要是为了在分页插件前面。这个是 MyBatis Plus 的规定
        MyBatisUtils.addInterceptor(interceptor, inner, 0);
        return inner;
    }


}
